*****************************************************************************************************
************                     Replication Code (Version 1.0) For:                   **************
************ Political Alignment and the Allocation of Stock Market Resources in China **************
*****************************************************************************************************

*** This .do file reproduces: Table 4 in the main text.

clear
set matsize 11000
set more off

use IPO_Rejection.dta 

********************************
***** Data Pre-processing  *****
********************************

replace gov_related = 0 if gov_related ==. & note !=""   
merge m:m id using Firm_Application.dta 
drop if _merge == 1 & gov_related ==.
drop if _merge == 2 & success <=.25  
drop _merge

gen symbol = substr(id,1,6)
gen yearend = year1
merge m:1 symbol yearend using Firm_Connection.dta
drop if _merge == 2
drop _merge

replace firm_pc = 0 if firm_pc ==.
replace pc_low = 0 if pc_low ==.
replace pc_city = 0 if pc_city ==.
gen pc_county = (pc_low == 1 & pc_city == 0)
gen firm_nlpc = (firm_pc == 1 & pc_county == 0)
replace npc = firm_nlpc

** Recode main variables 
gen fail_non_gov = 0  
replace fail_non_gov = 1 if gov_related == 0
gen fail_gov = 0   
replace fail_gov = 1 if gov_related == 1
gen fail_all_types = ( fail_non_gov == 1 | fail_gov == 1 | success !=1)

replace mcprom = connection_prom if mcprom ==.  
replace mcprom = connection_prom if success>.5 & success !=. & connection_prom !=. 
drop if mcprom ==. 
sum gov_related
bysort mcprom: sum(gov_related)

** Create fixed effects dummies
replace prov = province if prov == ""
egen ind_code = group(industry_csrc)
egen pro_code = group(province)
egen city_code = group(city)
egen board_code = group(board)   
egen sponsor_code = group(sponsor)

** Log transformation for firm and city attributes
gen lgfs=ln(firmsize0 + 1)
gen lgcapital = ln(registered_capital/1000 + 1)
replace gdp=gdppc*pop if gdp==.
gen lggdp=ln(gdp + 1)
gen lggdppc=ln(gdppc + 1)
gen lgpop=ln(pop + 1)
gen linv = log(investment + 1)
gen lgovrev = log(govrev + 1)
gen lgovexp = log(govexp + 1)
gen unerate = unemployment / (pop * 100)

***** Table 4 *****
global conn_var npc StateOwnedShares   
global firm_var roa0 roe0 lgfs age alratio0 lgcapital  
global city_var lgpop lggdp lggdppc gdpgrowth lgovrev lgovexp linv unerate  

*** Columns (1) to (3): Provincial Influence
eststo clear
eststo: qui reghdfe fail_gov mcprom if year1 <= 2016, a(city_code i.year0) vce(cluster city_code)  // city FE and application year FE
eststo: qui reghdfe fail_gov mcprom duration_y $firm_var $conn_var if year1 <= 2016, a(city_code i.year0 board_code ind_code i.year0##i.sponsor_code) vce(cluster city_code)  // application + firm attributes
eststo: qui reghdfe fail_gov mcprom duration_y $firm_var $conn_var $city_var if year1 <= 2016, a(city_code i.year0 board_code ind_code i.year0##i.sponsor_code) vce(cluster city_code)  // city attributes
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(mcprom) replace
estadd ysumm

*** Columns (4) to (6): Other Reasons 
eststo clear
eststo: qui reghdfe fail_non_gov mcprom if year1 <= 2016, a(city_code i.year0) vce(cluster city_code)  // city FE and application year FE
eststo: qui reghdfe fail_non_gov mcprom duration_y $firm_var $conn_var if year1 <= 2016, a(city_code i.year0 board_code ind_code i.year0##i.sponsor_code) vce(cluster city_code)  // application + firm attributes
eststo: qui reghdfe fail_non_gov mcprom duration_y $firm_var $conn_var $city_var if year1 <= 2016, a(city_code i.year0 board_code ind_code i.year0##i.sponsor_code) vce(cluster city_code)  // city attributes
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(mcprom) replace
estadd ysumm
